package com.cubeofnine.sugar;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import android.content.Context;
import android.graphics.Color;
import android.text.util.Linkify;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TableLayout.LayoutParams;

import com.cubeofnine.sugar.service.element.EntryResult;
import com.cubeofnine.sugar.service.element.EntryValue;
import com.cubeofnine.sugar.service.element.Field;
import com.cubeofnine.sugar.service.element.NameValue;
import com.cubeofnine.utils.ActivityParams;
import com.cubeofnine.utils.ScrollingTextView;

public class ContentAccountDynamicLayout {
	public static Context gContext = null;

	public void genContent(Context context, TextView headerTitle,
			LinearLayout root, EntryResult entryResult, String module_name,
			String module_id) {
		this.gContext = context;
		String fieldName = "";
		String fieldLabel = "";
		String fieldValue = "";

		LinearLayout contactRoot = null;
		LinearLayout.LayoutParams contactRootParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);// W,H
		contactRootParams.setMargins(10, 2, 10, 8);// left, top, right,
													// bottom

		LinearLayout.LayoutParams tabParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);// W,H
		// tabParams.setMargins(5, 0, 5, 0);// left, top, right, bottom

		LinearLayout.LayoutParams tabTextParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);// W,H
		tabTextParams.setMargins(10, 5, 0, 5);// left, top, right, bottom

		TextView textLabel = null;
		TextView textValue = null;
		LinearLayout.LayoutParams textValueLabel = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		textValueLabel.setMargins(10, 5, 0, 0);// left, top, right, bottom

		Map fieldMap = entryResult.getFieldList();

		Field field = null;
		EntryValue entryValue = entryResult.getEntryList().get(module_id);
		NameValue nameValue = null;

		Set fieldSet = fieldMap.keySet();
		Iterator fieldIter = fieldSet.iterator();

		Map overviewInfo = new HashMap();
		Map moreInfo = new HashMap();
		Map otherInfo = new HashMap();

		String name = "";

		String billing_address_street = "";
		String billing_address_city = "";
		String billing_address_state = "";
		String billing_address_postalcode = "";
		String billing_address_country = "";

		String shipping_address_street = "";
		String shipping_address_city = "";
		String shipping_address_state = "";
		String shipping_address_postalcode = "";
		String shipping_address_country = "";

		String date_entered = "";
		String created_by_name = "";
		String date_modified = "";
		String modified_by_name = "";

		List email = new Vector();
		Iterator iter = null;

		while (fieldIter.hasNext()) {
			field = (Field) fieldMap.get(((String) fieldIter.next()));
			fieldName = field.getName();
			fieldLabel = field.getLabel();

			Log.d("DynamicLayout - Contact", "field = " + field);
			Log.d("DynamicLayout - Contact", "fieldName = " + fieldName);
			Log.d("DynamicLayout - Contact", "fieldLabel = " + fieldLabel);

			nameValue = (NameValue) entryValue.getNameValueList()
					.get(fieldName);

			Log.d("DynamicLayout - Contact", "nameValue = " + nameValue);

			try {
				fieldValue = nameValue.getValue();
			} catch (Exception e) {
				Log.e("DynamicLayout - Contact", "Error", e);
				fieldValue = "";
			}

			Log.d("DynamicLayout - Contact", "fieldValue = " + fieldValue);

			if ("name".equals(fieldName)) {
				name = fieldValue;
				overviewInfo.put(1, new ActivityParams(fieldLabel, fieldValue));
			} else if ("website".equals(fieldName)) {
				overviewInfo.put(2, new ActivityParams(fieldLabel, fieldValue));
			} else if ("billing_address_street".equals(fieldName)) {
				billing_address_street = fieldValue;
			} else if ("billing_address_city".equals(fieldName)) {
				billing_address_city = fieldValue;
			} else if ("billing_address_state".equals(fieldName)) {
				billing_address_state = fieldValue;
			} else if ("billing_address_postalcode".equals(fieldName)) {
				billing_address_postalcode = fieldValue;
			} else if ("billing_address_country".equals(fieldName)) {
				billing_address_country = fieldValue;
			} else if ("shipping_address_street".equals(fieldName)) {
				shipping_address_street = fieldValue;
			} else if ("shipping_address_city".equals(fieldName)) {
				shipping_address_city = fieldValue;
			} else if ("shipping_address_state".equals(fieldName)) {
				shipping_address_state = fieldValue;
			} else if ("shipping_address_postalcode".equals(fieldName)) {
				shipping_address_postalcode = fieldValue;
			} else if ("shipping_address_country".equals(fieldName)) {
				shipping_address_country = fieldValue;
			} else if (fieldName.indexOf("email") != -1) {
				email.add(fieldValue);
			} else if ("phone_office".equals(fieldName)) {
				overviewInfo.put(6, new ActivityParams(fieldLabel, fieldValue));
			} else if ("phone_alternate".equals(fieldName)) {
				overviewInfo.put(7, new ActivityParams(fieldLabel, fieldValue));
			} else if ("phone_fax".equals(fieldName)) {
				overviewInfo.put(8, new ActivityParams(fieldLabel, fieldValue));
			} else if ("description".equals(fieldName)) {
				overviewInfo.put(9, new ActivityParams(fieldLabel, fieldValue));
			} else if ("account_type".equals(fieldName)) {
				moreInfo.put(1, new ActivityParams(fieldLabel, fieldValue));
			} else if ("annual_revenue".equals(fieldName)) {
				moreInfo.put(2, new ActivityParams(fieldLabel, fieldValue));
			} else if ("sic_code".equals(fieldName)) {
				moreInfo.put(3, new ActivityParams(fieldLabel, fieldValue));
			} else if ("parent_name".equals(fieldName)) {// Member of
				moreInfo.put(4, new ActivityParams(fieldLabel, fieldValue));
			} else if ("campaign_name".equals(fieldName)) {
				moreInfo.put(5, new ActivityParams(fieldLabel, fieldValue));
			} else if ("industry".equals(fieldName)) {
				moreInfo.put(6, new ActivityParams(fieldLabel, fieldValue));
			} else if ("employees".equals(fieldName)) {
				moreInfo.put(7, new ActivityParams(fieldLabel, fieldValue));
			} else if ("ticker_symbol".equals(fieldName)) {
				moreInfo.put(8, new ActivityParams(fieldLabel, fieldValue));
			} else if ("ownership".equals(fieldName)) {
				moreInfo.put(9, new ActivityParams(fieldLabel, fieldValue));
			} else if ("rating".equals(fieldName)) {
				moreInfo.put(10, new ActivityParams(fieldLabel, fieldValue));
			} else if ("assigned_user_name".equals(fieldName)) {
				otherInfo.put(1, new ActivityParams(fieldLabel, fieldValue));
			} else if ("date_entered".equals(fieldName)) {
				date_entered = fieldValue;
			} else if ("created_by_name".equals(fieldName)) {
				created_by_name = fieldValue;
			} else if ("date_modified".equals(fieldName)) {
				date_modified = fieldValue;
			} else if ("modified_by_name".equals(fieldName)) {
				modified_by_name = fieldValue;
			}
		}

		billing_address_street += billing_address_city + " "
				+ billing_address_state + " " + billing_address_postalcode
				+ " " + billing_address_country;
		billing_address_street = billing_address_street.replaceAll("  ", " ");
		overviewInfo.put(3, new ActivityParams("Billing Address:",
				billing_address_street));

		shipping_address_street += shipping_address_city + " "
				+ shipping_address_state + " " + shipping_address_postalcode
				+ " " + shipping_address_country;
		shipping_address_street = shipping_address_street.replaceAll("  ", " ");
		overviewInfo.put(4, new ActivityParams("Shipping Address:",
				shipping_address_street));

		overviewInfo.put(5, new ActivityParams("Email:", email));

		date_entered += " by " + created_by_name;
		otherInfo.put(2, new ActivityParams("Date Created:", date_entered));

		date_modified += " by " + modified_by_name;
		otherInfo.put(3, new ActivityParams("Date Modified:", date_modified));
		headerTitle.setText("Account Name : " + name);

		// Contact Root Layout
		contactRoot = new LinearLayout(context);
		contactRoot.setLayoutParams(tabParams);
		contactRoot.setOrientation(LinearLayout.VERTICAL);
		contactRoot.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

		// fieldLabel
		textLabel = new ScrollingTextView(context);
		textLabel.setLayoutParams(tabTextParams);// W,H
		textLabel.setText("Overview");
		textLabel.setTextColor(Color.WHITE);
		textLabel
				.setTextAppearance(context, android.R.attr.textAppearanceLarge);
		contactRoot.addView(textLabel);
		root.addView(contactRoot);

		ActivityParams params = null;
		for (int i = 1; i <= overviewInfo.size(); i++) {
			params = (ActivityParams) overviewInfo.get(i);
			// Contact Root Layout
			contactRoot = new LinearLayout(context);
			contactRoot.setLayoutParams(contactRootParams);
			contactRoot.setOrientation(LinearLayout.VERTICAL);

			// fieldLabel
			textLabel = new TextView(context);
			textLabel.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// W,H
			textLabel.setText(params.getName());
			textLabel.setTextColor(Color.rgb(170, 170, 170));// Gray
			textLabel.setTextAppearance(context,
					android.R.attr.textAppearanceSmall);
			// textLabel.setSingleLine(true);
			// textLabel.setHorizontallyScrolling(true);
			// textLabel.setEllipsize(TruncateAt.MARQUEE);
			// textLabel.setMarqueeRepeatLimit(1);
			// textLabel.setMovementMethod(new ScrollingMovementMethod());
			contactRoot.addView(textLabel);

			// fieldValue
			if (i == 5) {
				// Email
				email = (Vector) params.getValue();
				iter = email.iterator();
				while (iter.hasNext()) {
					textValue = new TextView(context);
					textValue.setLayoutParams(textValueLabel);
					textValue.setClickable(true);
					textValue.setAutoLinkMask(Linkify.ALL);
					textValue.setTextColor(Color.BLACK);
					textValue.setText(String.valueOf(iter.next()));
					textValue.setTextAppearance(context,
							android.R.attr.textAppearanceLarge);
					// textValue.setSingleLine(true);
					// textValue.setHorizontallyScrolling(true);
					// textValue.setEllipsize(TruncateAt.MARQUEE);
					// textValue.setMarqueeRepeatLimit(1);
					// textValue
					// .setMovementMethod(new ScrollingMovementMethod());
					contactRoot.addView(textValue);
				}
			} else {
				// Other
				textValue = new TextView(context);
				textValue.setLayoutParams(textValueLabel);

				if (i >= 2 && i <= 8) {
					textValue.setClickable(true);
					textValue.setAutoLinkMask(Linkify.ALL);
				}

				textValue.setText(String.valueOf(params.getValue()));
				textValue.setTextColor(Color.BLACK);
				textValue.setTextAppearance(context,
						android.R.attr.textAppearanceLarge);
				// textValue.setSingleLine(true);
				// textValue.setHorizontallyScrolling(true);
				// textValue.setEllipsize(TruncateAt.MARQUEE);
				// textValue.setMarqueeRepeatLimit(1);
				// textValue.setMovementMethod(new
				// ScrollingMovementMethod());
				contactRoot.addView(textValue);
			}

			root.addView(contactRoot);
		}

		// Contact Root Layout
		contactRoot = new LinearLayout(context);
		contactRoot.setLayoutParams(tabParams);
		contactRoot.setOrientation(LinearLayout.VERTICAL);
		contactRoot.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

		// fieldLabel
		textLabel = new ScrollingTextView(context);
		textLabel.setLayoutParams(tabTextParams);// W,H
		textLabel.setText("More Information");
		textLabel.setTextColor(Color.WHITE);
		textLabel
				.setTextAppearance(context, android.R.attr.textAppearanceLarge);
		contactRoot.addView(textLabel);
		root.addView(contactRoot);

		for (int i = 1; i <= moreInfo.size(); i++) {
			params = (ActivityParams) moreInfo.get(i);
			// Contact Root Layout
			contactRoot = new LinearLayout(context);
			contactRoot.setLayoutParams(contactRootParams);
			contactRoot.setOrientation(LinearLayout.VERTICAL);

			// fieldLabel
			textLabel = new ScrollingTextView(context);
			textLabel.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// W,H
			textLabel.setText(params.getName());
			textLabel.setTextColor(Color.rgb(170, 170, 170));// Gray
			textLabel.setTextAppearance(context,
					android.R.attr.textAppearanceSmall);
			// textLabel.setSingleLine(true);
			// textLabel.setHorizontallyScrolling(true);
			// textLabel.setEllipsize(TruncateAt.MARQUEE);
			// textLabel.setMarqueeRepeatLimit(1);
			// textLabel.setMovementMethod(new ScrollingMovementMethod());
			contactRoot.addView(textLabel);

			// fieldValue
			textValue = new ScrollingTextView(context);
			textValue.setLayoutParams(textValueLabel);
			textValue.setText(String.valueOf(params.getValue()));
			textValue.setTextColor(Color.BLACK);
			textValue.setTextAppearance(context,
					android.R.attr.textAppearanceLarge);
			// textValue.setSingleLine(true);
			// textValue.setHorizontallyScrolling(true);
			// textValue.setEllipsize(TruncateAt.MARQUEE);
			// textValue.setMarqueeRepeatLimit(1);
			// textValue.setMovementMethod(new ScrollingMovementMethod());
			contactRoot.addView(textValue);

			root.addView(contactRoot);
		}

		// Contact Root Layout
		contactRoot = new LinearLayout(context);
		contactRoot.setLayoutParams(tabParams);
		contactRoot.setOrientation(LinearLayout.VERTICAL);
		contactRoot.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

		// fieldLabel
		textLabel = new ScrollingTextView(context);
		textLabel.setLayoutParams(tabTextParams);// W,H
		textLabel.setText("Other");
		textLabel.setTextColor(Color.WHITE);
		textLabel
				.setTextAppearance(context, android.R.attr.textAppearanceLarge);
		contactRoot.addView(textLabel);
		root.addView(contactRoot);

		for (int i = 1; i <= otherInfo.size(); i++) {
			params = (ActivityParams) otherInfo.get(i);
			// Contact Root Layout
			contactRoot = new LinearLayout(context);
			contactRoot.setLayoutParams(contactRootParams);
			contactRoot.setOrientation(LinearLayout.VERTICAL);

			// fieldLabel
			textLabel = new ScrollingTextView(context);
			textLabel.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// W,H
			textLabel.setText(params.getName());
			textLabel.setTextColor(Color.rgb(170, 170, 170));// Gray
			textLabel.setTextAppearance(context,
					android.R.attr.textAppearanceSmall);
			// textLabel.setSingleLine(true);
			// textLabel.setHorizontallyScrolling(true);
			// textLabel.setEllipsize(TruncateAt.MARQUEE);
			// textLabel.setMarqueeRepeatLimit(1);
			// textLabel.setMovementMethod(new ScrollingMovementMethod());
			contactRoot.addView(textLabel);

			// fieldValue
			textValue = new ScrollingTextView(context);
			textValue.setLayoutParams(textValueLabel);
			textValue.setText(String.valueOf(params.getValue()));
			textValue.setTextColor(Color.BLACK);
			textValue.setTextAppearance(context,
					android.R.attr.textAppearanceLarge);
			// textValue.setSingleLine(true);
			// textValue.setHorizontallyScrolling(true);
			// textValue.setEllipsize(TruncateAt.MARQUEE);
			// textValue.setMarqueeRepeatLimit(1);
			// textValue.setMovementMethod(new ScrollingMovementMethod());
			contactRoot.addView(textValue);

			root.addView(contactRoot);
		}

		// Line
		// drawView = new DrawView(context);
		// drawView.setBackgroundColor(Color.BLACK);
		// contactRoot.addView(drawView);
	}
}
